import Vue from "vue";
import Vuex from "vuex";
import { apis } from '@/api/index.js';
import { user } from './modules/user';

Vue.use(Vuex);

const modules = {
	user
};

const state = {
	activeBid: null,
	env: ''
}

const mutations = {
	setActiveBid(state, data) {
		if (data) {
			state.activeBid = data
		}
	},
	setEnv(state, data) {
		state.env = data
	}
}

const actions = {
	// 读取并设置本地缓存
	setCacheData({ state, commit }) {
		commit('user/setCacheData');
	},
	// 清空内存缓存
	clearCache({ state, commit }) {
		commit('user/clear');
	},
	getActiveBid({ state, commit }) {
		return apis.bid.getActive({}, { isLoad: false, isPrompt: false })
			.then(res => {
				if (res) {
					console.log('bid:',res)
					commit('setActiveBid', res)
					return true
				} else {
					return false
				}
			})
	},
	initApp({ state, commit, dispatch }) {
		dispatch('clearCache');
		dispatch('setCacheData');
		console.log('initApp')
		return dispatch('user/getUserInfo')
			.then(res => {
				console.log('获取用户身份成功')
			})
			.catch(res => {
				console.error(res);
			})
	}
}

const store = new Vuex.Store({ modules, actions, state, mutations });

export default store;
